package com.cyt.work.kwmanagement.service.impl;

import cn.hutool.core.date.DateUtil;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.model.vo.SelectVo;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.DictCode;
import com.cyt.base.util.ErrorCode;
import com.cyt.base.util.StringUtils;
import com.cyt.sys.model.po.SysDict;
import com.cyt.sys.service.SysDictService;
import com.cyt.work.kwmanagement.dao.KwKmMessageDao;
import com.cyt.work.kwmanagement.model.po.KwKmMessage;
import com.cyt.work.kwmanagement.model.vo.KwKmMessageVo;
import com.cyt.work.kwmanagement.service.KwKmMessageService;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.service.impl.BaseServiceImpl;
import com.cyt.wx.model.po.WxEducationUser;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 科目表-科目表Service层实现类
 *
 * @author han jun
 * @date 2019-11-26 11:23:35
 */
@Service("kwKmMessageService")
public class KwKmMessageServiceImpl extends BaseServiceImpl<KwKmMessage> implements KwKmMessageService {

    @Resource
    private KwKmMessageDao kwKmMessageDao;

    @Resource
    private SysDictService sysDictService;

    @Autowired
    public void setBaseDao(KwKmMessageDao kwKmMessageDao) {
        this.baseDao = kwKmMessageDao;
    }

    /**
     * 查询对象集合
     *
     * @param kwKmMessageVo 对象
     * @return List 对象集合
     * @author han jun
     * @date 2019-11-26 11:23:35
     */
    @Override
    public List<KwKmMessage> listExample(KwKmMessageVo kwKmMessageVo) {
        Example example = new Example(KwKmMessage.class);
        example.setOrderByClause(" create_time desc");
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        criteria.andEqualTo("ksId", kwKmMessageVo.getKsId());
        return kwKmMessageDao.selectByExample(example);
    }




    /**
     * 分页查询
     *
     * @param kwKmMessageVo 接受参数对象
     * @param pageNum          分页数
     * @param pageSize         每页显示条数
     * @return ReturnResult
     * @author han jun
     * @date 2019-11-26 11:23:35
     */
    @Override
    public ReturnResult page(KwKmMessageVo kwKmMessageVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        List<KwKmMessage> list = listExample(kwKmMessageVo);
        PageInfo<KwKmMessage> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 添加或更新
     *
     * @param kwKmMessageVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author han jun
     * @date 2019-11-26 11:23:35
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdate(KwKmMessageVo kwKmMessageVo, SysUser sysUser) {
        if (StringUtils.isBlank(kwKmMessageVo.getId())) {
            KwKmMessage kwKmMessage = new KwKmMessage(sysUser);
            kwKmMessage.setApplyId(kwKmMessageVo.getApplyId());
            kwKmMessage.setKsId(kwKmMessageVo.getKsId());
            kwKmMessage.setKmmc(kwKmMessageVo.getKmmc());
            kwKmMessage.setBeginTime(DateUtil.parseDateTime(kwKmMessageVo.getBeginTime()));
            kwKmMessage.setEndTime(DateUtil.parseDateTime(kwKmMessageVo.getEndTime()));
            kwKmMessageDao.insert(kwKmMessage);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
        } else {
            KwKmMessage kwKmMessage = kwKmMessageDao.selectByPrimaryKey(kwKmMessageVo.getId());
            kwKmMessage.setApplyId(kwKmMessageVo.getApplyId());
            kwKmMessage.setKsId(kwKmMessageVo.getKsId());
            kwKmMessage.setKmmc(kwKmMessageVo.getKmmc());
            kwKmMessage.setBeginTime(DateUtil.parseDateTime(kwKmMessageVo.getBeginTime()));
            kwKmMessage.setEndTime(DateUtil.parseDateTime(kwKmMessageVo.getEndTime()));
            kwKmMessage.setUpdateTime(new Date());
            kwKmMessage.setUpdateUser(sysUser.getId());
            kwKmMessageDao.updateByPrimaryKey(kwKmMessage);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
        }
    }

    /**
     * 根据条件逻辑删除
     *
     * @param kwKmMessageVo 参数接受对象
     * @param sysUser 当前用户
     * @return ReturnResult
     * @author han jun
     * @date 2019-11-26 11:23:35
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult remove(KwKmMessageVo kwKmMessageVo, SysUser sysUser) {
        //要修改的字段
        KwKmMessage kwKmMessage = new KwKmMessage();
        kwKmMessage.setRemoveTag(DictCode.BASE_REMOVE_TAG_DELETE);
        kwKmMessage.setUpdateUser(sysUser.getId());
        kwKmMessage.setUpdateTime(new Date());
        //筛选条件
        Example example = new Example(KwKmMessage.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(kwKmMessageVo.getId())) {
            criteria.andIn("id", Arrays.asList(kwKmMessageVo.getId().split(",")));
        }
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        kwKmMessageDao.updateByExampleSelective(kwKmMessage, example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 根据条件物理删除
     *
     * @param kwKmMessageVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author han jun
     * @date 2019-11-26 11:23:35
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult delete(KwKmMessageVo kwKmMessageVo, SysUser sysUser) {
        //筛选条件
        Example example = new Example(KwKmMessage.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(kwKmMessageVo.getId())) {
            criteria.andIn("id", Arrays.asList(kwKmMessageVo.getId().split(",")));
        }
        kwKmMessageDao.deleteByExample(example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 根据考务id删除所有科目
     *
     * @param  sysUser 用户
     * @return ReturnResult
     * @author Jun Han
     * @date 2019/12/2 17:27
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult removeAll(KwKmMessageVo kwKmMessageVo, SysUser sysUser){
        KwKmMessage kwKmMessage = new KwKmMessage();
        kwKmMessage.setRemoveTag(DictCode.BASE_REMOVE_TAG_DELETE);
        kwKmMessage.setUpdateUser(sysUser.getId());
        kwKmMessage.setUpdateTime(new Date());
        //筛选条件
        Example example = new Example(KwKmMessage.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(kwKmMessageVo.getKsId())) {
            criteria.andEqualTo("ksId",kwKmMessageVo.getKsId());
        }
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        kwKmMessageDao.updateByExampleSelective(kwKmMessage, example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);

    }




}